package com.shujia.scala


object Demo12Map {
  def main(args: Array[String]): Unit = {

    /**
      * map   kv
      *
      */

    val map = Map("001" -> "张三", "002" -> "李四")


    map.foreach(kv => {
      println(kv._1 + "\t" + kv._2)
    })

    //通过key获取value
    val option: Option[String] = map.get("001")
    println(option.get)

    //通过key获取value
    val value = map("002")
    println(value)

    println(map.getOrElse("004", "默认值"))

    val keys = map.keys //获取key
    println(keys)

    println(map.values) //获取value


    println(map.toList) //将map转换成list

    //返回一个新的map
    val map1 = map.+("003" -> "王五")
    val map2 = map.+(("003", "王五"))

    println(map)
    println(map1)

    //不可变集合所在的包
    import scala.collection.immutable

    //可变集合
    import scala.collection.mutable
    val map3 = mutable.Map("001" -> "张三")
    map3.put("002", "李四") //增加元素
    println(map3)
    map3.remove("001") //删除元素
    println(map3)


  }
}
